<?php
/*

Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
$semf_root = "../../";

$table_name=$_GET['table_name'];
$table_id=$_GET['table_id'];
$module_name=$_GET['module_name'];
$project_home=$_GET['project_home'];

//print_r($_GET);
$relative_project_dir = $semf_root.$project_home;
$this_module_xml = $relative_project_dir.$module_name.'/module.xml';


//print_r($this_module_xml);

$i=0;
$inherited_table=array();
if (file_exists($this_module_xml)) {
	$doc = new DOMDocument();
	$doc->load($this_module_xml);
	
	$table_elements = $doc-> getElementsByTagName('table');
	
	$table_element = $table_elements -> item($table_id);

	//$field_list = $target_table-> childNodes;
	if($table_element->getAttribute("interface")){
	//echo 'here';
	//echo $table_id;
		$is_interface = $table_element->getAttribute("interface");
		if($is_interface == "yes") // never should be matched
		{
			//some code to handle interface -- anyway this shouldn't be displayed
			//echo 'interface';
			exit;
		}
	}
	$child_elements = $table_element->childNodes;
	//print_r($child_elements);
	foreach ($child_elements as $child_element)
	{
		if($child_element->tagName != "inherited")
		{
			continue;
		}
		//echo $child_element->tagName;
		$inherited_table[$i++] = trim($child_element->nodeValue);
	}
	$doc->save($this_module_xml);
}
//print_r($inherited_table);
$modules = array();
// Loop through all of the files:
$i = 0;
$d = dir($relative_project_dir);

while (false !== ($file = $d->read())) {
	// If the file is not this file, and does not start with a '.' or '~'
	$relative_path=$relative_project_dir.'/'.$file;
	if ( ($file{0} != '.') &&
		 ($file{0} != '~') &&
		 (substr($file, -3) != 'php') &&
		 is_dir($relative_path) )
		 {
			if($file == "store") //we ignore this..
			{
				continue;
			}
			$modules[$i++] = $file;
	}
}

// Skip current module for inheritance
//if($key=array_search($module_name,$modules)){
//	$modules[$key]=NULL;
//}
$interface_string='';
foreach ($modules as $module){
	$module_xml = $semf_root.$project_home.$module.'/module.xml';

	if (file_exists($module_xml)) {
		$doc = new DOMDocument();
		$doc->load($module_xml);
		if($doc == NULL || $doc->documentElement == NULL)
		{
			exit();
		}
		$table_elements = $doc->documentElement->childNodes;
		
		//print_r($inherited_table);
		foreach ($table_elements as $table_element)
		{
			if($table_element->nodeType != XML_ELEMENT_NODE)
			{
				continue;
			}
			if($table_element ->getAttribute('interface') == "yes")
			{
				$table_name_tmp = $table_element->getAttribute('name');
				$str = trim($module.'.'.$table_name_tmp);
				if(in_array($str,$inherited_table)){
					//echo $str;
					$checked='||yes';
				}else {
					$checked='||no';
				}
				$interface_string .= $module.'||'.$table_name_tmp.$checked.'==';
			}
		}
	}
	$doc->save($module_xml);
}
//remove the last comma
if(strlen($interface_string) > 1){
	$interface_string = substr($interface_string,0,strlen($interface_string)-2);
}
echo $interface_string;
?>